package com.tlias.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tlias.entity.Clazz;
import com.tlias.entity.ClazzQueryParam;
import com.tlias.entity.Emp;
import com.tlias.entity.PageBean;
import com.tlias.mapper.ClazzMapper;
import com.tlias.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

    //  分页查询
    @Override
    public PageBean clazzList(ClazzQueryParam param) {
        log.info("获取所有班级信息: {} {} {} {} {}",param.getPage(), param.getPageSize(), param.getName(), param.getBegin(), param.getEnd());
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<Clazz> clazzList = clazzMapper.clazzList(param);
        Page<Clazz> clazzPage = (Page<Clazz>) clazzList;
        return new PageBean(clazzPage.getTotal(), clazzPage.getResult());
    }
//    删除班级
    @Override
    public void deleteClazz(Integer id) {
        clazzMapper.deleteClazz(id);
        log.info("删除了id为{}的班级", id);
    }

    //添加班级
    @Override
    public void insertClazz(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        //查询一下班主任的id然后封装到集合里用来在添加班级的时候进行选择分配班主任
        clazzMapper.insertClazz(clazz);
        log.info("添加了班级: {}", clazz);
    }

    //根据id查询班级
    @Override
    public Clazz getClazz(Integer id) {
        return clazzMapper.getClazz(id);
    }

    //修改班级
    @Override
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.updateClazz(clazz);
    }

    //查询所有班级
    @Override
    public List<Clazz> findAllList() {
        return clazzMapper.findAllList();
    }

}
